package com.bwhx.eruler.controller.hodo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.casdilly.SizeRule;
import com.bwhx.eruler.bean.hodo.ClothesSize;
import com.bwhx.eruler.controller.BaseController;
import com.bwhx.eruler.service.hodo.ClothesSizeService;
import com.bwhx.eruler.util.Constant;
import com.bwhx.eruler.util.ListUtils;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/hodo_clothes_size")
public class ClothesSizeController extends BaseController {
	
	@Autowired
	ClothesSizeService clothesSizeService;

	
	 
	@ResponseBody
	@RequestMapping(value = "/page.list.ashx")
	 public Map<String,Object> sizeRulerList(Integer page,Integer rows,ClothesSize cSize) {
		

		
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,cSize);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<ClothesSize> list = clothesSizeService.list(p);
			Integer count = clothesSizeService.count(cSize);

					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	
	@ResponseBody
	@RequestMapping(value = "/page.detaillist.ashx")
	 public Map<String,Object> clothesSizeDetailList(Integer page,Integer rows,ClothesSize cSize) {
		

		
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,cSize);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<HashMap<String,String>> list = clothesSizeService.detailList(p);
			Integer count = clothesSizeService.detailCount(cSize);

					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	
	
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdate.ashx")
	public String saveOrUpdate(ClothesSize cSize) {
		try{
			
			Staff sessionUser = this.getSessionStaff();
			
			
			if(cSize.getCsID().equals("-1")){
				cSize.setCreatorID(sessionUser.getsId().toString());
				cSize.setCreateTime(new Date());
				List<ClothesSize> csList = clothesSizeService.findConflict(cSize);
				
				if(ListUtils.isNullOrEmpty(csList)){
					clothesSizeService.insert(cSize);
				}else{
					return "conflict";  
				}
			
				
				
			}else{
				
				cSize.setModifierID (sessionUser.getsId().toString());
				cSize.setModifyTime(new Date());
				
			
				List<ClothesSize> csList = clothesSizeService.findConflict(cSize);
				
				if(ListUtils.isNullOrEmpty(csList) || (csList.size()==1 &&csList.get(0).getCsID().equals(cSize.getCsID())) ){
					clothesSizeService.update(cSize);
				}else{
					return "conflict";  
				}

			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.del.ashx")
	public String del(ClothesSize cSize) {
		try{
			
			Staff sessionUser = this.getSessionStaff();	
			cSize.setModifierID (sessionUser.getsId().toString());
			cSize.setModifyTime(new Date());
			clothesSizeService.delete(cSize);
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	
	@ResponseBody
	@RequestMapping("/findByClothesSizeID.ashx")
	public HashMap<String,Object> findByClothesSizeID(HttpServletRequest request){
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		
		try {
			
			
			Object clothesSizeID = request.getAttribute("clothesSizeID");
			
			if(clothesSizeID instanceof String){
				List<Map<String,String> > cs = clothesSizeService.findByClothesSizeID((String) clothesSizeID);
				resultMap.put(Constant.DATA, cs);
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.MSG, Constant.MSG_SUCCESS);
				return resultMap;
			}
			
			if(null==clothesSizeID){
				 //下载全部的 成衣规格，以map存储，key为成衣规格id
				Map<String,Object> resultCSizeMap = clothesSizeService.findAllClothesSize(); 
				resultMap.put(Constant.DATA, resultCSizeMap);
				resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				resultMap.put(Constant.MSG, Constant.MSG_SUCCESS);
				return resultMap;
				
			}
		
			
		} catch (Exception e) {
			log.error(getTrace(e));
			resultMap.put(Constant.STATUS, Constant.STATUS_FAIL);
			resultMap.put(Constant.MSG, Constant.MSG_FAIL);
		}
		
		
		return resultMap;
		
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/page.listAll.ashx")
	 public List<ClothesSize> listAll() {
		
 		
		List<ClothesSize> list = clothesSizeService.listAll();
 			 
		
	    return list;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdateDetail.ashx",method = RequestMethod.POST)
	public String saveOrUpdateDetail(String params) {
		try{
			
			Staff sessionUser = this.getSessionStaff();
			
			HashMap<String,Object> paramMap = (HashMap<String,Object>) JSONObject.toBean(JSONObject.fromObject(params), HashMap.class);
   			if(paramMap.get("sizeid").equals("-1")){
				paramMap.put("creatorID", sessionUser.getsId().toString());
				paramMap.put("createTime", new Date() );
 				List<Map<String,Object>> csList = clothesSizeService.findDetailConflict(paramMap);
				
				if(ListUtils.isNullOrEmpty(csList)){
					clothesSizeService.insertDetail(paramMap);
				}else{
					return "conflict";  
				}
			
				
				
			}else{
				
			 
				paramMap.put("modifierID", sessionUser.getsId().toString());
				paramMap.put("modifyTime", new Date());
				
			
				List<Map<String,Object>> csList = clothesSizeService.findDetailConflict(paramMap);
				
				if(ListUtils.isNullOrEmpty(csList) || (csList.size()==1 &&csList.get(0).get("sizeid").toString().equals(paramMap.get("sizeid"))) ){
					clothesSizeService.updateDetail(paramMap);
				}else{
					return "conflict";  
				}

			} 
			 
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.delDetail.ashx")
	public String delDetail(String sizeid) {
		try{
			
			Staff sessionUser = this.getSessionStaff();	
			
			HashMap<String,Object> paramMap = new HashMap<String,Object>();
			paramMap.put("sizeid", sizeid);
			paramMap.put("modifierID", sessionUser.getsId().toString());
			paramMap.put("modifyTime", new Date());
			
			 
			clothesSizeService.delDetail(paramMap);
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }

}
